InformIT
Exact Phrase
All Words
Enter search string and press enter!

Search Tips

MyInformIT - Collect Your Own Content Infobases - Premium Subscription Content AlphaBooks - Content in Progress Editors' Choice - What the Experts Read Free Library - Complete Books Online InformIT Store - Books and Software Download It - Freeware and Shareware ZD News - IT News and Views BrassRing.com Jobs - Find Your Dream Job

DPEC Online Training
Click to Recommend-IT
Top IT News from ZD Net
Shareware from Tucows
 
Cisco Knowledge SuiteLinux/Open SourceWindows NTWindows 2000Visual Basic
ConfigureITNetworkITWebITDatabaseITProgramIT
 

You are here : Home : Tutorial 2: Programming Beyond Visual Basic Using the Windows API

Tutorial 2: Programming Beyond Visual Basic
Using the Windows API

by Bob Reselman and Richard Peasley - June 12, 2000

Tutorial 2: Programming Beyond VB Using Windows API

Understanding the Windows API

 

Working with the API Viewer

 

Monitoring Mouse Movement with GetCursorPos

 

Keeping a Window Always on Top by Using SetWindowPos

 
Source  
 

Monitoring Mouse Movement with GetCursorPos

Now that you've seen how to use the API Viewer to work more easily with the Windows API, let's put the function GetCursorPos to work in a real-world example.

GetCursorPos reports the position of the mouse pointer anywhere onscreen. Normally, a Visual Basic application is limited to reporting the position of the mouse pointer with the boundaries of itself, so the application has no idea where the cursor is when the mouse pointer is moved beyond a Visual Basic application's form. However, windows always knows where the mouse pointer is and reports the location through the GetCursorPos API function.

The project prjAPIStuff.vbp, which is available with the source text, illustrates this use of . Within the project, in the module modAPIStuff is a user-defined function, ReportMousePos(), which returns a string that reports back the location of the mouse pointer in friendly language. The function is a wrapper function for GetCursorPos. A wrapper function is one that primarily uses the functionality of another function, as if the function were wrapping itself around another. Listing 1 shows the structure of ReportMousePos().

Listing 1 ReportMousePos() Wraps GetCursorPos
01  Public Function ReportMousePos() As String
02
03      Dim MyPointAPI As POINTAPI 
04      Dim l As Long  
05      Dim strReport As String 
06
07      'Call the API function
08      l = GetCursorPos(MyPointAPI)
09
10      'Make a report string
11      strReport = "X=" & CStr(MyPointAPI.X) & ", "
12      strReport = strReport & "Y= " & CStr
(MyPointAPI.Y) 13 14 'Return the value of the report string 15 If l > 0 Then 16 ReportMousePos = strReport 17 Else 18 ReportMousePos = "Error" 19 End If 20 21 End Function

Notice that line 3 initializes POINTAPI. The actual declaration of the type and the GetCursorPos function were done in the General Declarations section of the module. Line 8 calls the GetCursorPos function, using the type instance MyPointAPI as an argument. Windows passes back the x and y location of the mouse pointer to the elements of MyPointAPI. If the value of l (the return buffer) is greater than 0, the call was successful. Lines 11 and 12 concatenate a string that reports back the pixel location of the mouse pointer with some "x:" and "y:" labeling. If the operation was successful (line 15), the reporting string is returned by the ReportMousePos() function. Otherwise, it returns an error string (line 18).

This function is called from within a Timer control's Timer() event procedure. The Timer() event procedure assigns the function's return value to the Caption property of a Label control. The event procedure is as follows:

Private Sub Timer1_Timer()
    lblMousePos.Caption = ReportMousePos()
End Sub

GetCursorPos does not report back the mouse pointer position unless called. Therefore, the Timer control is used to poll Windows every 1/10 of a second for the location of the mouse pointer. For more information about timers, see the source text in Chapter 16: "Working with Time and Timers."

 

Previous | Next

 

What's New in Visual Basic

Moving Components from MTS to COM+

Visual Basic and OOP: The Next Generation

Creating E-Mail Applications

Featured Author

Havoc Pennington Havoc Pennington

Havoc Pennington works for Red Hat Advanced Development Labs, where he enhances the Linux desktop development infrastructure and user experience. He is the author of several components of the GNOME GUI libraries, and is the primary architect of Gconf, a new GNOME configuration database library.

Featured Book

Sams Teach Yourself Shell Programming in 24 Hours Sams Teach Yourself Shell Programming in 24 Hours

Take control of your systems by harnessing the power of the shell.

 
 
 

About InformIT

|

Press Area

|

Advertising

|

Careers

Contact Us

|

Copyright, Terms & Conditions

|

Privacy Policy

 

© Copyright 2000 InformIT. All rights reserved.